# -*- coding: utf-8 -*-
#coding=utf-8

import web
import os
import config
import hashlib
from action.user.base import userAction
from model.user import user

'''
User controller: producing login views.
'''
class refererAction(userAction):
    def __init__(self, name = '登陆系统'):
        userAction.__init__(self, name, chkLogin=False, chkInstall=False)

    def check(self):
        from web import form
        validList=(
            form.Textbox("username", form.regexp(r".{3,20}$", 'User name: 3-20 chars')),
            form.Password("password", form.regexp(r".{3,20}$", 'Password: 3-20 chars')),
        )

        if not self.validates(validList):
            return self.error(self.errorMessage)

        inputData = self.getInput()
        password = hashlib.md5(inputData['password']).hexdigest()
        condition = {'email':inputData['username'],'passwd':password}
        userObj = user().getOne('*',condition)
        if userObj:
            self.setLogin(inputData['username'])
            return self.success(msg='登陆成功', url=self.makeUrl('/user'))
        else:
            return self.error('用户名或者密码不正确，请重新登陆', url=self.makeUrl('/user/login'))

    def POST(self, name):
        if name == 'check':
            return self.check()
        else:
            return self.notFound()
